package com.lixin.smartclassroom.ui.home.tab1.activity

import com.drake.brv.utils.bindingAdapter
import com.drake.net.utils.scopeNetLife
import com.lixin.smartclassroom.R
import com.lixin.smartclassroom.api.classhistorylist
import com.lixin.smartclassroom.api.deletemessage
import com.lixin.smartclassroom.api.getmessagelist
import com.lixin.smartclassroom.bean.DataListBean
import com.lixin.smartclassroom.common.saveSuccessDialog
import com.lixin.smartclassroom.databinding.ActivityMessageBinding
import com.lixin.smartclassroom.ui.home.adapter.messageList
import com.lixinkeji.base.BaseActivity
import com.lixinkeji.extension.getIntentString
import com.lixinkeji.extension.gone
import com.lixinkeji.extension.onClick
import com.lixinkeji.extension.removeAt
import com.lixinkeji.extension.start
import com.lixinkeji.extension.toast
import com.lixinkeji.extension.visible
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener

/**
 * Description 消息
 * Author: lwc
 * Date:2023/12/6 10:11
 */
class MessageAct : BaseActivity<ActivityMessageBinding>() {
    override fun getViewBinding() = ActivityMessageBinding.inflate(layoutInflater)

    override fun topBarHide() = false
    override fun startPadding() = true
    private lateinit var listBean: ArrayList<DataListBean>
    override fun title() = "消息通知"
    private var totalPage = 1
    private var page = 1

    override fun iniView() {
        listBean = ArrayList()

        binding.recycler.messageList().apply {
            R.id.btnDelete.onClick {
                scopeNetLife {
                    val baseBean = deletemessage(getModel<DataListBean>(modelPosition).id)
                    if (baseBean.result == "0") {
                        saveSuccessDialog("删除成功")
                        binding.recycler.bindingAdapter.removeAt(modelPosition)
                    }
                }
            }
        }.models = listBean

//        binding.apply {
//            refresh.onRefresh {
//                scopeNetLife {
//                    listBean = getmessagelist(index, 10)!!.dataList!!
//                    addData(listBean) {
//                        if (binding.recycler.bindingAdapter.models?.size == 0) {
//                            binding.state.showEmpty()
//                            binding.recycler.gone()
//                        } else {
//                            binding.recycler.visible()
//                        }
//                        listBean.size == 10
//                    }
//                }
//            }.autoRefresh()
//        }
        getMessageData()

        binding.smartRefreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                scopeNetLife {
                    page = 1
                    binding.smartRefreshLayout.finishRefresh()
                    listBean.clear()
                    getMessagePageData()
                }
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                if (page >= totalPage) {
                    refreshLayout.setNoMoreData(true)
                    return
                }
                page++
                getMessagePageData()
            }

        })

        binding.smartRefreshLayout.autoRefresh()

    }

    private fun getMessagePageData() {
        scopeNetLife {
            val baseBean = getmessagelist(page, 10)
            if (baseBean.result == "0") {
                binding.smartRefreshLayout.finishRefresh()
                binding.smartRefreshLayout.finishLoadMore()
                listBean.addAll(baseBean.dataList!!)
                binding.recycler.bindingAdapter.notifyDataSetChanged()
            }
        }
    }

    private fun getMessageData() {
        binding.apply {
            scopeNetLife {
                val baseBean = getmessagelist(1, 10)
                if (baseBean.result == "0") {
                    totalPage = baseBean.totalPage!!
                    if (baseBean.zhktcount?.isNotEmpty() == true && baseBean.zhktcount != "0") {
                        llZhkt.visible()
                        tvContentZhkt.text = baseBean.zhkttitle
                    } else {
//                        llZhkt.gone()
                    }

                    if (baseBean.ztcgcount?.isNotEmpty() == true && baseBean.ztcgcount != "0") {
                        llCgrw.visible()
                        tvContentCgrw.text = baseBean.ztcgtitle
                    } else {
//                        llCgrw.gone()
                    }
                    if (baseBean.yanxuecount?.isNotEmpty() == true && baseBean.yanxuecount != "0") {
                        llYxrw.visible()
                        tvContentYxrw.text = baseBean.yanxuetitle
                    } else {
//                        llYxrw.gone()
                    }
                    if (baseBean.xitongcount?.isNotEmpty() == true && baseBean.xitongcount != "0") {
                        llXtxx.visible()
                        tvContentXtxx.text = baseBean.xitongtitle
                    } else {
//                        llXtxx.gone()
                    }
                }
            }
        }

    }

    override fun onClick() {
        binding.apply {
            llXtxx.onClick {
                start(SystemMessageAct::class.java)
            }
            llZhkt.onClick {
                start(TaskMessageAct::class.java) {
                    putExtra("type", "3")
                }
            }
            llCgrw.onClick {
                start(TaskMessageAct::class.java) {
                    putExtra("type", "4")
                }
            }
            llYxrw.onClick {
                start(TaskMessageAct::class.java) {
                    putExtra("type", "5")
                }
            }
        }
    }
}